package com.sky.task;

import com.sky.controller.admin.WebSocketServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author chengjy
 * @date 2023/4/27
 */
@Component
@Slf4j
public class TaskDemo {

    @Autowired
    private WebSocketServer webSocketServer;

    private final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Scheduled(cron = "0/30 * * * * ?")
    public void test(){
        String name = Thread.currentThread().getName();
        log.info("线程名称: "+name);
        log.info("鸡你太美！！");
        log.info("蔡徐坤戳了你一下...喔~备备");
        log.info("迎面走来的你让我如此蠢蠢欲动...");
    }

    /**
     * 通过WebSocket每隔5秒向客户端发送消息
     */
    @Scheduled(cron = "0/5 * * * * ?")
    public void sendMessageToClient() {
        webSocketServer.sendToAllClient("这是来自服务端的消息：" + DateTimeFormatter.ofPattern("HH:mm:ss").format(LocalDateTime.now()));
    }
}
